package cn.admin.configcenter.registry.common.config;

import org.apache.commons.lang.StringUtils;

import java.io.File;
import java.lang.reflect.Field;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.*;

public class ConfigSetting {

    public static final Map<String, Field> configSettingFields = new HashMap<>();
    public static final String serverIp;
    public static boolean bindToAll;
    public static final int serverPort = 9600;
    public static volatile boolean isCombbingConnection;
    public static volatile String COMBBING_WHITELIST;
    public static int patternCacheCapacity;
    public static final String RULE_CONFIG_FILE_PATH;
    public static final String WEIGHTING_RULE_CONFIG_FILE_PATH;
    public static final String BLACKLIST_FILE_PATH;
    public static final String PERSIST_DATAID_WHITELIST_PATH;
    public static final String DEFAULT_GROUP = "DEFAULT_GROUP";
    public static long pushBufferSizeMB;
    public static String serverrole;
    public static List<String> unitserverlist;
    public static List<String> serverlist;
    public static long meanRange;
    public static boolean isOpHealthSwitch;
    public static final HashSet<String> citySeparateSet;
    public static int maxPubCountPerConnection;
    public static boolean ignorePubCountOverflow;
    public static int maxPubDataSizeInBytes;
    public static boolean ignorePubSizeOverflow;
    public static int maxSyncElementSizeInBytes;
    public static int maxClientConnections;
    public static boolean ignoreClientConnectionsOverflow;

    public static String getBindAddress() {
        String bindAddress = StringUtils.isBlank(serverIp) ? null : serverIp;
        if (bindToAll) {
            bindAddress = "0.0.0.0";
        }
        return bindAddress;
    }

    public static String getConfigServerHomePath() {
        String homePath = "";
        String jarUrlPath = ConfigSetting.class.getProtectionDomain().
                getCodeSource().getLocation().getFile();
        if (jarUrlPath.endsWith(".jar")  && jarUrlPath.contains("/lib/")) {
            homePath = new File(jarUrlPath).getParentFile().getAbsolutePath();
            homePath = homePath + (homePath.endsWith(File.separator) ? "" : File.separator);
        }
        return homePath;
    }

    static {
        Field[] fields = ConfigSetting.class.getDeclaredFields();
        for (int i = 0;i < fields.length;i ++) {
            Field f = fields[i];
            f.setAccessible(true);
            configSettingFields.put(f.getName(),f);
        }

        String SERVER_HOME;
        try {
            SERVER_HOME = InetAddress.getLocalHost().getHostAddress();
        } catch (UnknownHostException e) {
            throw new RuntimeException(e);
        }
        serverIp = SERVER_HOME;
        bindToAll = true;
        isCombbingConnection = false;
        COMBBING_WHITELIST = "172\\.(19|23|24|25)\\..*";
        patternCacheCapacity = 1000;
        pushBufferSizeMB = 80L;
        unitserverlist = new ArrayList<>();
        serverlist = new ArrayList<>();
        meanRange = 10L;
        isOpHealthSwitch = true;
        citySeparateSet = new HashSet<>();
        maxPubCountPerConnection = 800;
        ignorePubCountOverflow = true;
        maxPubDataSizeInBytes = 2048;
        ignorePubSizeOverflow = false;
        maxSyncElementSizeInBytes = 51200;
        maxClientConnections = 8000;
        ignoreClientConnectionsOverflow = true;
        try {
            SERVER_HOME = getConfigServerHomePath();
            System.out.println("########## server home = " + SERVER_HOME);
            String confPath = SERVER_HOME + ".." + File.separator + ".." + File.separator + "conf" + File.separator;
            RULE_CONFIG_FILE_PATH = confPath + "config-rules.xml";
            System.out.println("########## RULE_CONFIG_FILE_PATH  = " + RULE_CONFIG_FILE_PATH);
            WEIGHTING_RULE_CONFIG_FILE_PATH = confPath + "weighting_config-rules.xml";
            System.out.println("########## WEIGHTING_RULE_CONFIG_FILE_PATH  = " + WEIGHTING_RULE_CONFIG_FILE_PATH);
            BLACKLIST_FILE_PATH = confPath + "blacklist.txt";
            System.out.println("########## BLACKLIST_FILE_PATH  = " + BLACKLIST_FILE_PATH);
            PERSIST_DATAID_WHITELIST_PATH = confPath + "persist-dataid-whitelist.txt";
            System.out.println("########## PERSIST_DATAID_WHITELIST_PATH  = " + PERSIST_DATAID_WHITELIST_PATH);
            citySeparateSet.add("com.taobao.forest.master.configId");
            citySeparateSet.add("com.taobao.forest.slave.configId");
            citySeparateSet.add("com.taobao.millau.config.master");
            citySeparateSet.add("com.taobao.millau.config");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

}
